<?php
class FriendlinkController extends BackendController
{

    public $active = "plugins-friendlink";
    public $plug_version = "1.0";
    public $plug_title = "友情链接";
    public $plug_description = "友情链接";
    public $plug_controller = "friendlink";

    public function actionIndex(){

        $action_type = isset($_REQUEST['action_type'])?$_REQUEST['action_type']:null;

        if($action_type == 'insert' || $action_type =='updatedo'){
            $plug_friendlink_id = $_REQUEST['plug_friendlink_id'];
            $title = $_REQUEST['title'];
            $imageurl = $_REQUEST['imageurl'];
            $linkurl = $_REQUEST['linkurl'];
            $seq = $_REQUEST['seq'];

            if($plug_friendlink_id == 0){
                $this->connection->createCommand("insert into rk_plug_friendlink (title, imageurl, linkurl,seq) values
 (:title, :imageurl, :linkurl,:seq)")
                    ->bindParam(":title",$title)
                    ->bindParam(":imageurl",$imageurl)
                    ->bindParam(":linkurl",$linkurl)
                    ->bindParam(":seq",$seq)
                    ->execute();
            }else{
                $this->connection->createCommand("update rk_plug_friendlink set title = :title,imageurl =:imageurl,linkurl=:linkurl,seq=:seq 
where plug_friendlink_id = :plug_friendlink_id ")
                    ->bindParam(":title",$title)
                    ->bindParam(":imageurl",$imageurl)
                    ->bindParam(":linkurl",$linkurl)
                    ->bindParam(":seq",$seq)
                    ->bindParam("plug_friendlink_id",$plug_friendlink_id)
                    ->execute();
            }

        }else if($action_type == 'update'){
            $plug_friendlink_id = $_REQUEST['plug_friendlink_id'];
            $this->data['update_friendlink'] = $this->connection->createCommand("select * from rk_plug_friendlink where plug_friendlink_id = :plug_friendlink_id ")
                ->bindParam("plug_friendlink_id",$plug_friendlink_id)
                ->queryRow();
        }else if($action_type == 'delete'){
            $plug_friendlink_id = $_REQUEST['plug_friendlink_id'];
            $this->connection->createCommand("delete from rk_plug_friendlink where plug_friendlink_id = :plug_friendlink_id ")
                ->bindParam("plug_friendlink_id",$plug_friendlink_id)
                ->execute();
        }

        $this->data['friendlinks'] = $this->connection->createCommand("select * from rk_plug_friendlink order by seq desc")->queryAll();

        $this->render("index",$this->data);
    }


    public function actionInstall(){

        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_friendlink`")->execute();
        $this->connection->createCommand("CREATE TABLE `rk_plug_friendlink` (
  `plug_friendlink_id` int(11) NOT NULL AUTO_INCREMENT,
  `imageurl` varchar(255) DEFAULT NULL,
  `linkurl` varchar(255) DEFAULT NULL,
  `seq` varchar(255) DEFAULT NULL,
  `createtime` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
  `title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`plug_friendlink_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8")->execute();
        $this->connection->createCommand("insert into rk_plug (plug_controller,plug_version,plug_key) values (:controller,:version,:key)")
            ->bindParam(":controller",$this->plug_controller)
            ->bindParam(":version",$this->plug_version)
            ->bindParam(":key",$this->active)
            ->execute();
        $parentMenuId = $this->connection->createCommand("SELECT menu_id FROM rk_menu WHERE menu_key = 'plugins'")->queryScalar();
        $this->connection->createCommand("INSERT INTO rk_menu ( menu_text, parent_id, url, menu_key ) VALUES (:menu_text,:parent_id,'index.php?r=plugins/friendlink',:menu_key)")
            ->bindParam(":menu_text",$this->plug_title)
            ->bindParam(":parent_id",$parentMenuId)
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }

    public function actionUninstall(){
        $this->connection->createCommand("delete from rk_plug where plug_key = :plug_key")
            ->bindParam(":plug_key",$this->active)
            ->execute();
        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_service`")->execute();
        $this->connection->createCommand("delete from rk_menu where menu_key = :menu_key")
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }

}